const app = getApp();

Page( {
    data: {
        username: null,
        pwd: null,
        modelContent: ''
    },
    onLoad: function() {

    },
    onUsernameInput: function( e ) {
        const _this = this;
        _this.setData( { username: e.detail.value });
        console.log(e.detail.value)
    },
    onPwdInput: function( e ) {
        const _this = this;
        _this.setData( { pwd: e.detail.value });
        console.log(e.detail.value)
    },
    onLoginTap: function( e ) {
        const _this = this;
        wx.request( {
            method: 'GET',
            url: 'http://localhost:8989/user/login',
            data: { username: _this.data.username, password: _this.data.pwd },
            success: function( res ) {
                if( res.data.code === 10000 ) {
                    // app.globalData.user = res.data.data.user;
                    // app.globalData.user.photo = domain + app.globalData.user.photo;
                    console.log("登录成功")
                    // 存储数据
                    wx.setStorage({
                        key: 'user',
                        data: res.data.data,
                        success: function () {
                            console.log('数据存储成功');
                        }
                    });
                    console.log("用户缓存成功")
                    wx.switchTab({
                      url: '/pages/home/index/index',
                    })
                } else {
                    _this.setData( { modelContent: res.data.msg });
                }
            }
        });
    },
    onModalConfirm: function( e ) {
        const _this = this;
        _this.setData( { modelContent: '' });
    },
    goReg:function(){
      wx.navigateTo({
        url: '/pages/user/reg/page',
      })
    }
});